/*
@hehung
2023-2-8
转载请注明出处，版权由@hehung所有
email: 1398660197@qq.com
wechat: hehung95
*/

#include "hal_data.h"
#include "app_touch.h"
#include "qe_touch_config.h"
#include "qe_touch_define.h"

#include "app_display.h"
#include "app_led.h"

#define TOUCH_DEBUG
#undef TOUCH_DEBUG


#ifdef TOUCH_DEBUG
#include <stdio.h>
#define LOG(fmt, ...) 	          printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...)
#endif

typedef enum
{
    TOUCH_BUTTON_PRESSED = 0,
    TOUCH_BUTTON_RELEASED
} e_TbStaType;


#define TOUCH_BUTTON_NUM            (2U)
#define TOUCH_BUTTON_1              (0U)
#define TOUCH_BUTTON_2              (1U)
// touch button flags
#define TOUCH_BUTTON_ID_1              (1U)
#define TOUCH_BUTTON_ID_2              (2U)


e_TbStaType touch_btn_sta[2] = {TOUCH_BUTTON_RELEASED, TOUCH_BUTTON_RELEASED};
e_TbStaType last_touch_btn_sta[2] = {TOUCH_BUTTON_RELEASED, TOUCH_BUTTON_RELEASED};



static void Touch_PressStatus(uint8_t touch_num);

// touch button status of pressing
static void Touch_PressStatus(uint8_t touch_num)
{
	if ((TOUCH_BUTTON_PRESSED == touch_btn_sta[touch_num]) && (TOUCH_BUTTON_RELEASED == last_touch_btn_sta[touch_num]))
	{
		// touch button pressed
        if (TOUCH_BUTTON_1 == touch_num)
        {
            Disp_Change();
        }
        else
        {   
            
        }
	}
	else if ((TOUCH_BUTTON_RELEASED == touch_btn_sta[touch_num]) && (TOUCH_BUTTON_PRESSED == last_touch_btn_sta[touch_num]))
	{
		// touch button released
        
	}
	else  if ((TOUCH_BUTTON_PRESSED == touch_btn_sta[touch_num]) && (TOUCH_BUTTON_PRESSED == last_touch_btn_sta[touch_num]))
	{
        Led_LED_3_PwmSet();
	}
}


// Initialize the touch module, TS1 and TS2
void Touch_Init(void)
{
    fsp_err_t err;

    /* Open Touch middleware */
    err = RM_TOUCH_Open(g_qe_touch_instance_config01.p_ctrl, g_qe_touch_instance_config01.p_cfg);
#ifdef TOUCH_DEBUG
    if (FSP_SUCCESS != err)
    {
        LOG("touch: Open touch module error!\n");
    }
	else
	{
		LOG("touch: Initialze finished\n");
	}
#else
	assert(FSP_SUCCESS == err);
#endif
}

// touch button function entry
void Touch_RunningEntry(void)
{
    fsp_err_t err;
	uint64_t button_status;

    /* for [CONFIG01] configuration */
    err = RM_TOUCH_ScanStart(g_qe_touch_instance_config01.p_ctrl);
#ifdef TOUCH_DEBUG
    if (FSP_SUCCESS != err)
    {
	    LOG("touch: Scan touch status error!\n");
    }
#else
	assert(FSP_SUCCESS == err);
#endif
    while (0 == g_qe_touch_flag) {}
    g_qe_touch_flag = 0;

    err = RM_TOUCH_DataGet(g_qe_touch_instance_config01.p_ctrl, &button_status, NULL, NULL);
    if (FSP_SUCCESS == err)
    {
        /* TODO: Add your own code here. */
        if(TOUCH_BUTTON_ID_1 == button_status)
        {
#ifdef TOUCH_DEBUG
            LOG("touch: touch button TS1 pressed\n");
#endif
            touch_btn_sta[TOUCH_BUTTON_1] = TOUCH_BUTTON_PRESSED;
        }
        else if(TOUCH_BUTTON_ID_2 == button_status)
        {
#ifdef TOUCH_DEBUG
            LOG("touch: touch button TS2 pressed\n");
#endif
            touch_btn_sta[TOUCH_BUTTON_2] = TOUCH_BUTTON_PRESSED;
			
        }
        else
        {
            touch_btn_sta[TOUCH_BUTTON_1] = TOUCH_BUTTON_RELEASED;
            touch_btn_sta[TOUCH_BUTTON_2] = TOUCH_BUTTON_RELEASED;
        }

        Touch_PressStatus(TOUCH_BUTTON_1);
        Touch_PressStatus(TOUCH_BUTTON_2);

        last_touch_btn_sta[TOUCH_BUTTON_1] = touch_btn_sta[TOUCH_BUTTON_1];
        last_touch_btn_sta[TOUCH_BUTTON_2] = touch_btn_sta[TOUCH_BUTTON_2];
    }
}


